package com.zzld.modules.autoinn.action;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Coupon;
import com.zzld.modules.autoinn.service.ICollarService;
import com.zzld.modules.autoinn.service.IConponService;
import com.zzld.modules.common.dto.CommonJSON;

@Controller
@RequestMapping("/autoinn/coupon")
public class CouponController {

	@Autowired
	private IConponService couponService;
	
	@Autowired
	private ICollarService collarService;

	private String listurl = "/modules/autoinn/coupon/couponlist";
	private String updaturl = "/modules/autoinn/coupon/couponCru";

	@RequestMapping("/")
	public String list() {
		return listurl;
	}

	@RequestMapping(value = "/update")
	public String upda(Model model, HttpServletRequest request) {
		return updaturl;
	}

	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON del(Model model, HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		String id = request.getParameter("id");;
		try {
			
				
				// 执行删除操作
				if (couponService.deleteByPrimaryKey(id) > 0) {
					json.setSuccess(true);
					json.setMessage(MessageUtils.DELETE_ALL_SUCCESS);
				}
		
		} catch (Exception e) {

			json.setMessage(MessageUtils.DELETE_WARNING);
			json.setSuccess(false);
		}
		return json;
	}

	@RequestMapping(value = "/update/pro/json", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON update(Model model, HttpServletRequest request)
			throws ParseException {
		CommonJSON json = new CommonJSON();

		Coupon coupon = new Coupon();
		coupon.setCouponId(request.getParameter("couponId"));
		coupon.setCouponName(request.getParameter("couponName"));
		coupon.setCouponType(Integer.parseInt(request
				.getParameter("couponType")));
		coupon.setCouponPrice(BigDecimal.valueOf(Double.valueOf(request
				.getParameter("couponPrice"))));
		coupon.setCoupinDes(request.getParameter("coupinDes"));
		coupon.setCouponCount(Integer.parseInt(request
				.getParameter("couponCount")));
		coupon.setState(Byte.valueOf(request.getParameter("state")));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		coupon.setStartime(sdf.parse(request.getParameter("startime")));
		coupon.setEndtime(sdf.parse(request.getParameter("endtime")));
		coupon.setSouponQi(Integer.parseInt(request.getParameter("souponQi")));
		coupon.setCouponZhi(Integer.parseInt(request.getParameter("couponzhi")));

		if (StringUtil.isBlank(coupon.getCouponId())) {

			// 新增
			coupon.setCouponId(UuidUtil.uuid().toString());
			coupon.setCreateTime(new Date());
			try {
				if (couponService.saveSelective(coupon) > 0) {
					json.setMessage(MessageUtils.ADD_SUCCESS);
					json.setSuccess(true);
				}
			} catch (Exception e) {
				json.setMessage(MessageUtils.ADD_FAIL);
				json.setSuccess(false);
			}
		} else {
			// 修改
			try {
				if (couponService.updateByPrimaryKeySelective(coupon) > 0) {
					json.setMessage(MessageUtils.EDIT_SUCCESS);
					json.setSuccess(true);
				}
			} catch (Exception e) {
				json.setMessage(MessageUtils.EDIT_WARING);
				json.setSuccess(false);
			}
		}

		return json;
	}

	@RequestMapping(value = "/update/ser", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject ser(Model model, HttpServletRequest request) {
		JSONObject json = new JSONObject();

		String id = "";
		Coupon coupon = null;

		try {
			id = request.getParameter("id");
			coupon = couponService.selectByPrimaryKey(id);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("couponId", coupon.getCouponId());
			map.put("couponName", coupon.getCouponName());
			map.put("couponType", coupon.getCouponType());
			map.put("couponPrice", coupon.getCouponPrice());
			map.put("couponCount", coupon.getCouponCount());
			map.put("coupinDes", coupon.getCoupinDes());
			map.put("state", coupon.getState());

			
			map.put("startime", datzhuan(coupon.getStartime().toString()));
			map.put("endtime", datzhuan(coupon.getEndtime().toString()));

			map.put("souponQi", coupon.getSouponQi());
			map.put("couponzhi", coupon.getCouponZhi());
			json.putAll(map);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return json;
	}
	
	public String datzhuan(String da){
		SimpleDateFormat sdf1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
		
		 String sDate=null;
		try {
			Date date = sdf1.parse(da);
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			sDate=sdf.format(date);
		} catch (ParseException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
         
         return sDate;
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public CommonJSON list(Model model, HttpServletRequest request) {
		CommonJSON json = new CommonJSON();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", request.getParameter("name"));
		map.put("startime", request.getParameter("startime"));
		map.put("endtime", request.getParameter("endtime"));
		map.put("type", request.getParameter("type"));
		map.put("state", request.getParameter("state"));
		map.put("sort", request.getParameter("sort"));
		map.put("order", request.getParameter("order"));

		String PageNum = request.getParameter("page") == null ? "0" : request
				.getParameter("page");

		String PageSize = request.getParameter("rows") == null ? "10" : request
				.getParameter("rows");

		PageInfo page = couponService.list(map, Integer.parseInt(PageNum),
				Integer.parseInt(PageSize));
		
		json.setRows(page.getList());
		json.setTotal(page.getTotal());

		return json;
	}
}
